package org.wheel.plugins.toolkit.sql.segment;

import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.wheel.plugins.toolkit.sql.core.SqlSegment;

import java.util.List;

@Getter
@AllArgsConstructor
public class OrderBySegment implements SqlSegment {

    private final List<SqlSegment> columns;
    private final boolean asc;

    public static OrderBySegment asc(SqlSegment... segments) {
        return new OrderBySegment(CollUtil.newArrayList(segments), true);
    }
    public static OrderBySegment desc(SqlSegment... segments) {
        return new OrderBySegment(CollUtil.newArrayList(segments), false);
    }

    @Override
    public String getSql() {
        return CollUtil.join(columns, ", ", seg -> seg.getSql() + (asc ? " ASC" : " DESC"));
    }
}